#!/bin/bash

target=STARTDOC

file=$1
3<$file

copy=0
printed=0

matches()
{
    echo $line | grep $1 > /dev/null 2>&1
    return $?
}


targetre=`echo "^<.*$target.*>[[:space:]]*$"`
emptyre='^<>[[:space:]]*$'
elsere='^<else>[[:space:]]*$'
otherre='^<[^>]*>[[:space:]]*$'
blankre='^[[:space:]]*$'

while :
do
    read -u 3 -r line || break

    if matches $targetre ; then
        copy=1
    elif matches $emptyre ; then
        [ $printed != 0 ] && echo ""
        exit 0;
    elif [ $copy != 0 ] ; then
        echo $line
        printed=1
    fi
done

[ $copy != 0 ] && echo "Maybe no closing tag <> in $file" >&2
exit 0
